package com.lujianfei.android_app_list

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.lujianfei.android_app_list.base.BaseActivity
import com.lujianfei.android_app_list.base.BaseFragment

class MainActivity : BaseActivity() {

    private var mCurrentFragment: BaseFragment?= null
    private val mAppGridFragment by lazy { AppGridFragment() }

    override fun resouceId(): Int = R.layout.activity_main

    override fun initView() {
        switchFragment(mAppGridFragment)
    }

    override fun initData() {}

    override fun initEvent() {}

    private fun switchFragment(targetFragment: BaseFragment) {
        val beginTransaction = supportFragmentManager.beginTransaction()
        if (mCurrentFragment != targetFragment) {
            mCurrentFragment?.let {
                beginTransaction.hide(it)
            }
            if (targetFragment.isAdded) {
                beginTransaction.show(targetFragment)
            } else {
                beginTransaction.add(R.id.container, targetFragment)
            }
            mCurrentFragment = targetFragment
        }
        beginTransaction.commitAllowingStateLoss()
    }
}
